package 优先算法.模拟.Z字形变换;

/**
 *https://leetcode.cn/problems/zigzag-conversion/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        String s = "PAYPALISHIRING";
//        String s = "\"PAHNAPLSIIGYIR\"";
        String rel = so.convert(s, 3);
        System.out.println(rel);
    }
}
class Solution {
    public String convert(String ss, int numRows) {
        if(numRows == 1) return ss;
        char[] s = ss.toCharArray();
        int d = 2 * numRows -2;//计算公差
        StringBuilder rel = new StringBuilder();
        for(int i = 0; i < numRows; i++){
            if(i == 0 || i == numRows-1){
                int temp = i;
                while(temp < s.length){
                    rel.append(s[temp]);
                    temp+=d;
                }
                continue;
            }
            int temp1 = i,temp2 = d-i;
            while(temp2 < s.length || temp1 < s.length){
                if(temp1 < s.length){
                    rel.append(s[temp1]);
                }
                if(temp2 < s.length){
                    rel.append(s[temp2]);
                }
                temp1 +=d;
                temp2 +=d;
            }
        }

        return rel.toString();
    }
}